package cz.najman.emdroid.old;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.content.ContentValues;
import cz.najman.emdroid.content.ContentHandler;
import cz.najman.emdroid.mappings.PaymentInformation;
import cz.najman.emdroid.utils.XmlToDatabaseMapper;

/**
 * @author  Lukáš Najman
 */
public class PaymentInformationContentHandler extends ContentHandler {

	private int index;
	/**
	 */
	private static XmlToDatabaseMapper mMappings;
	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		index = mMappings.getCorrespondingIndex(qName);
		
		
			switch (index) {
			case 100:
				mBulider.delete(0, mBulider.length());
				mValues = new ContentValues();
				mValues.put(PaymentInformation.VALUE_TYPE, atts.getValue(0));
				break;			
			default:
				mBulider.delete(0, mBulider.length());
				break;
			}		
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		index = mMappings.getCorrespondingIndex(qName);		
		
		switch (index) {
		case 100:
			//mListener.onParsedDataReceive(mValues, mRequestId);
			break;
		case PaymentInformation.INDEX_ACTIVE_FROM:			
			Long activeFrom = getTimeFromString(mBulider.toString());			
			mValues.put(PaymentInformation.ACTIVE_FROM, activeFrom);
			break;
		case PaymentInformation.INDEX_ACTIVE_TO:
			Long activeTo = getTimeFromString(mBulider.toString());
			mValues.put(PaymentInformation.ACTIVE_TO, activeTo);
			break;
		case PaymentInformation.INDEX_VALUE:
			mValues.put(PaymentInformation.VALUE, mBulider.toString());
			break;
		case PaymentInformation.INDEX_ID:
			mValues.put(PaymentInformation._ID, mBulider.toString());	
			break;
		case PaymentInformation.INDEX_EVENT_ID:
			mValues.put(PaymentInformation.EVENT_ID, mBulider.toString());
			break;
		default:
			break;
		}		
		
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		mBulider.append(ch, start, length);
	}
	
	static {
		
		mMappings = new XmlToDatabaseMapper();		
		mMappings.putMapping("activeFrom", PaymentInformation.INDEX_ACTIVE_FROM);
		mMappings.putMapping("activeTo", PaymentInformation.INDEX_ACTIVE_TO);
		mMappings.putMapping("value", PaymentInformation.INDEX_VALUE);
		mMappings.putMapping("id", PaymentInformation.INDEX_ID);
		mMappings.putMapping("paymentInformation", 100);
		mMappings.putMapping("eventId", PaymentInformation.INDEX_EVENT_ID);
		
	}
	
}
